package com.jeeplus.modules.sys.service;

import com.jeeplus.common.utils.DbUtils;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.service.BaseService;
import com.jeeplus.modules.sys.entity.Office;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.test.one.dialog.entity.Leave1;
import com.jeeplus.modules.tools.utils.MultiDBUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Field;
import java.sql.JDBCType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 * @title: ReportService
 * @projectName jeeplus
 * @description: TODO
 * @date 2019/7/1513:23
 */
@Service
@Transactional(readOnly = true)
public class ReportService extends BaseService {
    @Autowired
    DbUtils dbUtils;
    /*获取机构信息*/
    protected String getOfficeCode(String officeId)
    {
        List<String> list=new ArrayList<>();
        for (Office off:UserUtils.getOfficeList()){
            for (String p:officeId.split(",")){
                if(off.getId().contains(p)){
                    if(!list.contains(off.getCode())&&!off.getCode().isEmpty())
                    {
                        list.add(off.getCode());
                    }
                }
            }
        }
        return StringUtils.join(list.toArray(),",");
    }
    /**
     * 描述
     *
     * @Author LH
     * @Description //TODO
     * @Date 16:02 2019/7/16
     * @param page
     * @param leave1
     * @return com.jeeplus.core.persistence.Page<com.jeeplus.modules.test.one.dialog.entity.Leave1>
     **/
    public Page<Leave1> findPage(Page<Leave1> page, Leave1 leave1) {
        //dataRuleFilter(leave1);
        NamedParameterJdbcTemplate jdbcTemplate=new NamedParameterJdbcTemplate(MultiDBUtils.get("bjgc").getJdbcTemplate());
        Map<String,Object> params = new HashMap<String,Object>();
        String sql="select rownum as id,run_date," +
                "       loc," +
                "       store_name        STORE_NAME," +
                "       group_no," +
                "       group_name," +
                "       供应商号          suppl_no," +
                "       供应商名称        suppl_name," +
                "       经营方式          suppl_type," +
                "       item," +
                "       p.local_item_desc item_descr," +
                "       barcode," +
                "       p.含税售价        sell_pr," +
                "       p.畅销品标识      is_cxp," +
                "       p.kvi标识         is_kvi," +
                "       p.菜篮子标识      is_clz," +
                "       p.产地            Origin," +
                "       p.保质期          expriy_days," +
                "       p.保存条件        saveopt," +
                "       p.商品状态        item_status," +
                "       p.是否可销售      is_sale," +
                "       p.新品标识        is_xp," +
                "       p.可用库存        stock," +
                "       p.当前库存        curr_stock," +
                "       p.suppl_open " +
                "  from pjc_px p  ";
        if(!leave1.getOffice().getId().isEmpty())
        {
            params.put("store",Integer.valueOf(getOfficeCode(leave1.getOffice().getId())));
            sql+=" where loc = :store";
        }
       try {
           page.setCount(jdbcTemplate.queryForObject(dbUtils.getCountString(sql),params,Integer.class));
       }catch (Exception e){
           System.out.println(e.getMessage());
       }

        //sql="select * from ("+sql+") t where t.id >:rowStart and id <=:rowEnd";
        sql= new DbUtils().getLimitString(sql,page.getPageNo(),page.getPageSize());
        params.put("rowStart",page.startRow());
        params.put("rowEnd", page.endRow());
        List<Map<String,Object>> list=jdbcTemplate.queryForList(sql,params);
        page.setListData(list);
        return page;
        //return super.findPage(page, leave1);
    }
}
